package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.BusPayFlowno;
import com.fahai.web.service.CallRecordService;
import com.fahai.web.service.ConvenientService;
import com.fahai.web.vo.CallRecordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 方便大师
 */

@Controller
@RequestMapping("/convenient")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ConvenientController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(ConvenientController.class);

    @Autowired
    ConvenientService convenientService;

    /**
     * 返回界面
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','方便大师-0','方便大师-1')")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String enterCallPage(Model model){
        model.addAttribute("selectTab","方便大师");
        return "convenient_flow";
    }

    /**
     * 查看通话记录列表
     * @param pageVo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','方便大师-0','方便大师-1')")
    @ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public JsonResult findCallRecordList(PageVo pageVo, Model model, BusPayFlowno busPayFlowno){
        if(null!=busPayFlowno.getStartTimeTo()&&busPayFlowno.getStartTimeTo()!=""){
            busPayFlowno.setStartTimeTo(busPayFlowno.getStartTimeTo()+" 23:59:59");
        }
        PageVo pageVoData = convenientService.findList(busPayFlowno,pageVo);
        return ok("获取方便大师数据",pageVoData);
    }

    /**
     * 分页总数
     *
     * @param model
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','方便大师-0','方便大师-1')")
    @ResponseBody
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    public JsonResult listcount(PageVo pageVo, Model model, BusPayFlowno busPayFlowno) throws IOException {
        if(null!=busPayFlowno.getStartTimeTo()&&busPayFlowno.getStartTimeTo()!=""){
            busPayFlowno.setStartTimeTo(busPayFlowno.getStartTimeTo()+" 23:59:59");
        }
        int count= convenientService.getCount(busPayFlowno, pageVo);
        pageVo.setTotal( Long.valueOf(count));
        model.addAttribute("count", pageVo);
        return ok("查询", pageVo.getTotal());
    }


}
